import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import kotlinx.coroutines.launch
import v3.LiRunBiLi
import v3.LiRunCompose3
import v3.LiRunShouJia

@Composable
@Preview
fun App() {
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    var rememberCoroutineScope = rememberCoroutineScope()

    var selectedItem by remember { mutableStateOf(ModelV3.entries.first()) }

    ModalDrawer(
        drawerContent = {
            Column {
                Text("侧边导航", modifier = Modifier.padding(16.dp))
                Divider()
                ModelV3.entries.forEach {
                    DrawerItem(it.tag) {
                        rememberCoroutineScope.launch {
                            selectedItem = it
                            drawerState.close()
                        }
                    }
                }
            }
        },
        gesturesEnabled = true,
        drawerState = drawerState
    ) {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text(selectedItem.tag) },
                    navigationIcon = {
                        IconButton(onClick = { rememberCoroutineScope.launch { drawerState.open() } }) {
                            Icon(Icons.Filled.Menu, contentDescription = "打开侧边栏")
                        }
                    }
                )
            }
        ) { padding ->
            Box(Modifier.padding(padding)) {
                when (selectedItem) {
                    ModelV3.JiSuanBiLi -> LiRunBiLi()
                    ModelV3.JiSuanLiRun -> LiRunCompose3()
                    ModelV3.JiSuanShouJia -> LiRunShouJia()
                }
            }
        }
    }
}

@Composable
fun DrawerItem(text: String, onClick: () -> Unit) {
    TextButton(
        onClick = onClick,
        modifier = Modifier.fillMaxWidth()
    ) {
        Text(text)
    }
}

fun main() = application {
    Window(onCloseRequest = ::exitApplication) {
        App()
    }
}

/**
 * **通用输入框组件**
 */
@Composable
fun InputField(label: String, value: String, onValueChange: (String) -> Unit) {
    TextField(
        value = value,
        onValueChange = onValueChange,
        label = { Text(label) },
        keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
        modifier = Modifier.fillMaxWidth()
    )
}
